package www.sofn.com.domain;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@Data
@NoArgsConstructor
public class R<T> {
    @Schema( description = "状态")
    private CodeType type;
    @Schema(description = "消息")
    private String message;
    @Schema(description= "返回对象")
    private T Data;

    public static R ok() {
        return ok(null);
    }

    public static <T> R ok(T data) {
        return new R(CodeType.SUCCESS, CodeType.SUCCESS.text, data);
    }


    public static R error() {
        return error(CodeType.ERROR.text, null);
    }

    public static R error(String msg) {
        return error(msg, null);
    }

    public static <T> R error(T data) {
        return error(CodeType.ERROR.text, data);
    }

    public static <T> R error(String msg, T data) {
        return new R(CodeType.ERROR, msg, data);
    }

    public static <T> R unauthorized() {
        return unauthorized(CodeType.UNAUTHORIZED.text, null);
    }

    public static <T> R unauthorized(String msg) {
        return unauthorized(msg, null);
    }

    public static <T> R unauthorized(T data) {
        return unauthorized(CodeType.UNAUTHORIZED.text, data);
    }

    public static <T> R unauthorized(String msg, T data) {
        return new R(CodeType.UNAUTHORIZED, msg, data);
    }


    @AllArgsConstructor
    public enum CodeType {
        SUCCESS("成功"), ERROR("失败"), UNAUTHORIZED("未认证");
        private String text;
    }

}
